<?php


namespace app\api\controller\v9;


use app\api\controller\v2\Auth;
use app\common\cachemodel\ArticleRead;
use app\common\model\ArticleComment;
use app\common\model\ZlAstr;
use app\common\model\ZlUserAttention;
use app\common\lib\exception\ApiException;
use think\Exception;
use think\response\Json;

class Article extends Auth
{

    /**
     * 文章详情接口
     * @return Json
     * @throws ApiException
     * @throws Exception
     * @throws \Exception
     */
    public function details()
    {
        $access_token = input('access_token/s', '');// 处理旧版文章详情接口 文章发布时间为datetime

        $map = [
            'id' => input('param.id/d', 0),
            'uid' => $this->user['id'],
            'ucid' => $this->user['uc_id'],
            'is_old' => $access_token == '' ? true : false,
        ];

        $access_token == '' ? $time_type = 0 : $time_type = 1;

        $data = (new \app\common\model\Article())->getArticleDetails($this->user['id'], $map, $time_type);
        if ($data)
        {
            $astrInfo = (new ZlAstr())->getInfoById($data['auid']);

            $isSelf = $astrInfo['uid'] == $this->user['id'] ? 1 : 0;

            $attentionStatus = $isSelf ? 0 : (new ZlUserAttention())->andAstrStatusOfAttention($this->user['id'], $astrInfo['uid']);

            if ($this->user)
            {
                //内容已读
                (new ArticleRead())->saveData((int)$this->user['id'], 1, $map['id']);
            }

            $articleDetail = [
                'id' => $data['id'],
                'title' => $data['title'],
                'author' => $data['author'],
                'avatar' => $data['avatar'],
                'publish_time' => $data['publish_time'],
                'introduction' => $data['introduction'],
                'good' => $data['good'],
                'collect' => $data['collect'],
                'content' => $data['content'],
                'good_status' => $data['good_status'],
                'collect_status' => $data['collect_status'],
                'auid' => $data['auid'],
                'attention_status' => $attentionStatus,
                'astr_level' => $astrInfo['astr_level'] ?? 0,
                'is_inviter' => $astrInfo['is_inviter'] ?? 0,
                'is_self' => $isSelf,
            ];
            return show(config('code.success'), '加载成功', $articleDetail);
        }
        return show(config('code.error'), '内容不存在');
    }

    /**
     * 文章评论列表
     * @return Json
     * @throws \Exception
     */
    public function comment_list()
    {
        $aid = input('param.aid/d');
        $page = input('param.page/d', 1);
        // 缓存
        $data_list = (new ArticleComment())->getComments([
            'aid' => $aid,
            'page' => $page,
            'uc_id' => $this->user['uc_id'],
            'uid' => $this->user['id'],
        ]);

        $astrModel = new ZlAstr();

        $commentDatas = [];
        foreach ($data_list['data'] as $commentItem)
        {
            $d = [
                'id' => $commentItem['id'],
                'content' => $commentItem['content'],
                'create_time' => $commentItem['create_time'],
                'goods' => $commentItem['goods'],
                'reply' => $commentItem['reply'],
                'nickname' => $commentItem['nickname'],
                'avatar' => $commentItem['avatar'],
                'is_owner' => $commentItem['is_owner'],
                'good_status' => $commentItem['good_status'],
                'auid' => $commentItem['auid'],
                'is_astr' => 0,
                'astr_level' => 0,
                'is_inviter' => 0,
                'reply_list' => $commentItem['reply_list'],
            ];

            if ($commentItem['auid'])
            {
                $astrInfo = $astrModel->getInfoById($commentItem['auid']);
                $d['is_astr'] = $astrInfo['is_astr'];
                $d['astr_level'] = $astrInfo['astr_level'];
                $d['is_inviter'] = $astrInfo['is_inviter'];
            }

            $commentDatas[] = $d;
        }

        $data_list['data'] = $commentDatas;

        return show(config('code.success'), '操作成功', $data_list);
    }
}
